package dome_004;

import java.util.concurrent.TimeUnit;

/**
 * <p>Description: [一个同步方法可以调用另一个同步方法，一个线程已经拥有某个对象的锁，
 *      再次申请的时候仍然会得到该对象的锁
 *      也就是说synchronized获得的锁是可重入的]</p>
 * Created on 2020/12/3
 *
 * @author <a href="mailto: 819712047@qq.com">谢振东 </a>
 * @version 1.0
 */
public class Dome004_03 {

    synchronized void test1() {
        System.out.println("test1 start.........");
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        test2();
    }

    synchronized void test2() {
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("test2 start.......");
    }

    private void test3() {
        synchronized (this) {
            System.out.println("test3 start.........");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            test4();
        }
    }

    private void test4() {
        synchronized (this) {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("test4 start.......");
        }
    }

    public static void main(String[] args) {
        Dome004_03 dome004_03 = new Dome004_03();
        dome004_03.test1();
        dome004_03.test3();
    }
}
